﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using ParseControls;

namespace ParseOnlineAdmin
{
    /// <summary>
    /// Interaction logic for HostWindow.xaml
    /// </summary>
    public partial class HostWindow : Window, IDisposable
    {
        // ctr 
        public HostWindow()
        {
            InitializeComponent();

            ParseOne.Instance.ButtonClick += Instance_ButtonClick;
            ParseOne.Instance.InformationSend += Instance_InformationSend;
        }
        ~HostWindow()
        {
            this.Dispose();
        }

        // Events 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
        void Instance_ButtonClick(object sender, ParseCustomEventArgs e)
        {
            if (e.IsHandled)
                return;

            if (e.Command == "App:Config:View:Show")
            {
                ParseAppsConfigView view = new ParseAppsConfigView();
                AddControlToPanel(view);

                e.IsHandled = true;
                return;
            }

            if (e.Command == "Class:Add:View:Show")
            {
                AddParseClass view = new AddParseClass();
                AddControlToPanel(view);

                e.IsHandled = true;
                return;
            }
        }
        void Instance_InformationSend(object sender, ParseCustomEventArgs e)
        {
            if (e.IsHandled)
                return;

            if (e.Command == "Panel:Close")
            {
                PanelClose();
            }

            if (e.Command == "Assign:Related:Item:View")
            {
                ParseAddRelatedItem view = new ParseAddRelatedItem(e.Parameter);
                AddControlToPanel(view);

                e.IsHandled = true;
                return;
            }

            if (e.Command == "Task:Edit:View:Show")
            {
                ParseEditTask view = new ParseEditTask((ParseObjectMask)e.Object);
                AddControlToPanel(view);

                e.IsHandled = true;
                return;
            }

            if (e.Command == "Account:Show")
            {
                TheAccountView view = new TheAccountView();
                AddControlToPanel(view);

                e.IsHandled = true;
                return;
            }
        }

        // Methods 
        public void AddControlToPanel(Control ctl)
        {
            this.panel.Child = null;

            this.panel.Child = ctl;
        }
        public void PanelClose()
        {
            this.panel.Child = null;
        }

        // overrides 
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            LoadAccount();

            // Refresh 
            ParseOne.Instance.InformationSend += (sender, args) =>
            {
                if (args.IsHandled)
                    return;

                if (args.Command == "Account:Refresh")
                {
                    LoadAccount();
                }
            };
        }

        private void LoadAccount()
        {
            if (ParseOne.Package.BackgroundImageUrl != string.Empty)
            {
                this.bgImage.Source = new BitmapImage(new Uri(ParseOne.Package.BackgroundImageUrl));
            }
        }
        public void Dispose()
        {
            try
            {
                ParseOne.Instance.ButtonClick -= Instance_ButtonClick;
            }
            catch (Exception ex)
            {

            }
        }
    }
}
